Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text

Namespace C4F.DevKit.Calendar.Parser
	Friend Class DateParser
		#Region "Private Static Members"

		Private Shared utcFormat As String = "yyyyMMdd\THHmmss\Z"

		Private Shared localDateFormat As String = "yyyyMMdd"

		Private Shared localDateTimeFormat As String = "yyyyMMdd\THHmmss"

		Private Shared localFormats As String() = {localDateFormat, localDateTimeFormat}

		#End Region

		#Region "Public Static Methods"

		Public Shared Function ParseUTC(ByVal line As String) As DateTime
			Return DateTime.SpecifyKind(DateTime.ParseExact(line, utcFormat, Nothing), DateTimeKind.Utc)
		End Function

		Public Shared Function ParseLocalTime(ByVal line As String, ByVal offset As Integer) As DateTime
			Dim localDate As DateTime = DateTime.ParseExact(line, localFormats, Nothing, System.Globalization.DateTimeStyles.None)

			If localDate.IsDaylightSavingTime() Then
				offset = offset + 1
			End If

			Return DateTime.SpecifyKind(localDate.AddHours(-1 * offset), DateTimeKind.Utc)
		End Function

		Public Shared Function ParseDate(ByVal line As String) As DateTime
			Return DateTime.ParseExact(line, localDateFormat, Nothing)
		End Function

		Public Shared Function ToUTCString(ByVal time As DateTime) As String
			Return time.ToUniversalTime().ToString(utcFormat)
		End Function

		Public Shared Function ToDateString(ByVal time As DateTime) As String
			Return time.ToString(localDateFormat)
		End Function

		#End Region
	End Class
End Namespace
